{% assign twitter_pixels = storeConfig.twitter_pixel_id | strtoarray %}

{% if twitter_pixels %}
  <script>
  !function(e,t,n,s,u,a){e.twq||(s=e.twq=function(){s.exe?s.exe.apply(s,arguments):s.queue.push(arguments);
  },s.version='1.1',s.queue=[],u=t.createElement(n),u.async=!0,u.src='https://static.ads-twitter.com/uwt.js',
  a=t.getElementsByTagName(n)[0],a.parentNode.insertBefore(u,a))}(window,document,'script');
  {% for pixel in twitter_pixels %}
      twq('config',"{{pixel}}");
  {% endfor %}

  twq('track','PageView');

  {%- if controller == 'order' and action == 'success' -%}
    {% capture pixels %}twitter_pixels_{{order.id}}{% endcapture %}
    {% get_cache key={pixels} var_name=twitter_pixels_status%}
    {% if tiktok_pixels_status ==0 %}
      twq('track','Purchase', {
        value: {{order.total_price}},
        currency: '{{ order.currency_code }}',
      });
    {%- endif -%}
  {%- endif -%}

  {%- if template == 'order_payment_gateway' and order.payment_type == 'offlinepay' and storeConfig.is_offlinepay_purchase_pixel == '1' -%}
    {% capture pixels %}twitter_pixels_{{order.id}}{% endcapture %}
    {% get_cache key={pixels} var_name=twitter_pixels_status%}
    {% if tiktok_pixels_status ==0 %}
      twq('track','Purchase', {
        value: {{order.total_price}},
        currency: '{{ order.currency_code }}',
      });
    {%- endif -%}
  {%- endif -%}


  document.addEventListener("DOMContentLoaded",function(){
    moi.addEvent('callback_add_to_cart',function(event){
      console.log(event);
      twq('track','AddToCart');
    })

    moi.addEvent('callback_generate_lead',function(){
        twq('track','SignUp');
    })

    moi.addEvent('callback_begin_checkout',function(){
        twq('track','CheckoutInitiated');
    })
  })

</script>
{% endif %}

